package com.recharge.controller;

import com.recharge.base.ResponseInfo;
import com.recharge.base.ResponseInfoEnum;
import com.recharge.wk.domain.Product;
import com.recharge.wk.mapper.IProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by qi.cao on 2017/1/23.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductMapper iProductMapper;

    @RequestMapping("/queryList")
    @ResponseBody
    public ResponseInfo queryList(
            @RequestParam("productId") String productId,
            @RequestParam("productName") String productName,
            @RequestParam("curr") Integer curr,
            @RequestParam("pageSize") Integer pageSize,
            HttpServletRequest request) {
        ResponseInfo responseInfo = new ResponseInfo();
        List<Product> products = iProductMapper.selectList(productId, productName, curr, pageSize);
        responseInfo.setCount(iProductMapper.count(productId, productName));
        responseInfo.setData(products);
        return responseInfo;
    }

    @RequestMapping("/updateInfo")
    @ResponseBody
    public ResponseInfo updateInfo(
            @RequestParam("productId") String productId,
            @RequestParam("priority") Boolean priority,
            @RequestParam("weight") Boolean weight,
            @RequestParam("enable") Boolean enable,
            HttpServletRequest request) {
        ResponseInfo responseInfo = new ResponseInfo();
        responseInfo.setInfoEnum(ResponseInfoEnum.SUCCESS);
        Product product = new Product();
        product.setProductId(productId);
        product.setEnable(enable);
        product.setPriority(priority);
        product.setWeight(weight);
        iProductMapper.update(product);
        responseInfo.setCount(1);
        return responseInfo;
    }

    @RequestMapping("/addInfo")
    @ResponseBody
    public ResponseInfo addInfo(
            @RequestParam("productName") String productName,
            @RequestParam("priority") Boolean priority,
            @RequestParam("weight") Boolean weight,
            @RequestParam("enable") Boolean enable,
            HttpServletRequest request) {
        ResponseInfo responseInfo = new ResponseInfo();
        Product product = new Product();
        product.setProductName(productName);
        product.setEnable(enable);
        product.setPriority(priority);
        product.setWeight(weight);
        iProductMapper.insert(product);
        responseInfo.setInfoEnum(ResponseInfoEnum.SUCCESS);
        return responseInfo;
    }
}
